package com.huishao.demo.timer;

import com.huishao.demo.service.EmailService;
import com.huishao.demo.service.KeyService;
import com.huishao.demo.utils.PwdGenerate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

import static com.huishao.demo.common.EmailCommon.*;


@Slf4j
@Component
public class TaskUtils {

    @Resource
    EmailService emailService;

    @Resource
    KeyService keyService;


    // 每天发送随机 key 到邮箱 并记录进数据库
    @Scheduled(cron = "0 30 5 * * ? ",zone = "Asia/Shanghai") // cron表达式：每天的 5:30 执行一次命令
    public void doTask() throws Exception {
        String pwd = PwdGenerate.generate();
        emailService.sendKeyEmail(MY_EMAIL,
                TO_EMAIL,
                CC,
                KEY_SUBJECT,
                pwd
        );
        keyService.insertKey(PwdGenerate.encryption(pwd));
        log.info("key已发送！时间是 " + new Date());
    }


}
